//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductSaleDao;
import com.aidong.dao.wills.PssProductSaleListDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductSale;
import com.aidong.model.wills.PssProductSaleList;
import com.aidong.model.wills.PssProductSaleListTotal;
import com.aidong.service.PssPrintDataService;
import com.aidong.util.ExportXls;
import com.aidong.util.StockUtil;
import com.aidong.web.controller.pss.PopProductController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"productSaleList"})
public class ProductSaleListController extends PopProductController {
    @Resource
    PssProductSaleDao pssProductSaleDao;
    @Resource
    PssProductSaleListDao pssProductSaleListDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    StockUtil stockUtil;
    @Resource
    PssPrintDataService pssprintDataService;

    public ProductSaleListController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pss/productsale/list")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleDateFrom",required = false) String saleDateFrom, @RequestParam(value = "saleDateTo",required = false) String saleDateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "a.ShopCode", shopCode);
        if(!this.chkNull(saleDateFrom)) {
            cd.ge("a.SaleDate", this.parseDate(saleDateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(saleDateTo)) {
            cd.le("a.SaleDate", this.parseDate(saleDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.pssProductSaleListDao.getLstSale(pg, cd);
        ModelAndView mv = new ModelAndView("pss/productsale/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("ProductcodeList", this.getTypeList("ProductcodeList"));
        mv.addObject("userName", this.getTypeList("userName"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleDateFrom",required = false) String saleDateFrom, @RequestParam(value = "saleDateTo",required = false) String saleDateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "a.ShopCode", shopCode);
            if(!this.chkNull(saleDateFrom)) {
                cd.ge("a.SaleDate", this.parseDate(saleDateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(saleDateTo)) {
                cd.le("a.SaleDate", this.parseDate(saleDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }

            if(this.queryOwn()) {
                ConditionMap xls = new ConditionMap(1);
                xls.eq("c.salesCode", this.getLoginUserCode());
                xls.eq("c.salesCode2", this.getLoginUserCode());
                xls.eq("c.createUser", this.getLoginUserCode());
                cd.addSub(xls);
            }

            this.pssProductSaleListDao.getLstSale(e, cd);
            ExportXls xls1 = new ExportXls(response, "商品销售流水", "商品销售流水");
            xls1.addHead(new String[]{"门店编号", "门店名称", "销售单号", "销售日期", "商品编码", "商品名称", "商品分类", "单位", "数量", "单价", "金额", "销售员", "操作员"});
            xls1.setWidth(new int[]{10, 12, 10, 20, 10, 30, 10, 10, 10, 10, 10, 15, 15});
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                PssProductSaleListTotal sale = (PssProductSaleListTotal)i$.next();
                xls1.addRow(new Object[]{sale.getShopCode(), this.cache.getValue("shopName", sale.getShopCode()), sale.getAutoId(), sdf2.format(sale.getSaleDate()), sale.getProductCode(), this.cache.getValue("ProductcodeList", sale.getProductCode()), this.cache.getValue("productType", sale.getProductType()), this.cache.getValue("productUnit", sale.getUnit()), sale.getQuantity(), sale.getPrice(), sale.getAmount(), this.nvl(sale.getSaleCode()) + " - " + this.nvl(this.cache.getValue("salesName", sale.getSaleCode())), this.nvl(sale.getUserCode()) + " - " + this.nvl(this.cache.getValue("userName", sale.getUserCode()))});
            }

            xls1.close();
            this.addUserLog("导出商品销售流水", e.getResultList().size() + "条记录");
        } catch (Exception var11) {
            this.log.error("import", var11);
        }

        return null;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssProductSale pssProductSale = new PssProductSale();
        pssProductSale.setShopCode(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pss/productsale/edit");
        mv.addObject("pssProductSale", pssProductSale);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("shopCode", this.getLoginShopCode());
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("allowType", this.getTypeList("allowType"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssProductSale") PssProductSale pssProductSale, @RequestParam(value = "money",required = false) Double money, @RequestParam("payls") String payls, Products products) {
        if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请选择要销售的商品");
        } else {
            try {
                Date e = new Date();
                pssProductSale.setShopCode(this.getLoginShopCode());
                pssProductSale.setSaleDate(e);
                pssProductSale.setCardCode("");
                pssProductSale.setMemberCode("");
                pssProductSale.setSaleAmount(money);
                pssProductSale.setCreateUser(this.getLoginUserCode());
                pssProductSale.setCreateTime(e);
                pssProductSale.setUpdateTime(e);
                PssProductSaleList pss = null;
                ArrayList lst = new ArrayList();
                Iterator payList = products.getLstProduct().iterator();

                while(payList.hasNext()) {
                    PssPopProduct payMsg = (PssPopProduct)payList.next();
                    pss = new PssProductSaleList();
                    if(!this.chkNull(payMsg.getProductCode())) {
                        if(!this.stockUtil.checkStock(this.getLoginShopCode(), payMsg.getProductCode(), payMsg.getQty())) {
                            return this.getResult(false, "请确认商品：" + payMsg.getProductCode() + "的库存！");
                        }

                        pss.setShopCode(this.getLoginShopCode());
                        pss.setProductCode(payMsg.getProductCode());
                        pss.setPrice(payMsg.getAllowancePrice());
                        pss.setQuantity(payMsg.getQty());
                        pss.setMemo(payMsg.getMemo());
                        pss.setCreateUser(this.getLoginUserCode());
                        pss.setCreateTime(e);
                        pss.setUpdateTime(e);
                        lst.add(pss);
                    }
                }

                if(!this.chkNullZ(pssProductSale.getEarnestId())) {
                    MemberEarnest payList1 = (MemberEarnest)this.memberEarnestDao.getById(pssProductSale.getEarnestId());
                    if(payList1 == null || payList1.getStatus().intValue() != 0 || !payList1.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                        return this.getResult(false, "定金不可用");
                    }

                    pssProductSale.setEarnestAmount(payList1.getEarnestAmount());
                    pssProductSale.setMemberCode(payList1.getMemberCode());
                    pssProductSale.setCardCode(payList1.getCardCode());
                } else {
                    pssProductSale.setEarnestId((Integer)null);
                    pssProductSale.setEarnestAmount(Double.valueOf(0.0D));
                }

                ArrayList payList2 = new ArrayList();
                StringBuilder payMsg1 = new StringBuilder();
                PayList pay = this.getPayList(payls, payList2, "03", payMsg1);
                if(pay == null) {
                    return this.getResult(false, payMsg1.toString());
                }

                if(!this.equals(pay.getAmount().doubleValue(), pssProductSale.getSaleAmount().doubleValue() - pssProductSale.getEarnestAmount().doubleValue())) {
                    return this.getResult(false, "付款金额不一致");
                }

                pssProductSale.setSaleCode(pay.getSalesCode());
                this.pssProductSaleDao.addSale(pssProductSale, lst, this.pssProductStockDao, payList2);
                this.addUserLog("销售", "销售单号:" + pssProductSale.getAutoId());
            } catch (Exception var11) {
                this.log.error("add pssProductSale", var11);
                return this.getResult(false, "销售发生错误");
            }

            return this.getResult(true, "销售成功");
        }
    }

    @RequestMapping({"print"})
    private ModelAndView print(@RequestParam(value = "money",required = false) String money, @RequestParam("payls") String payls, Products products) {
        StringBuilder msg = new StringBuilder();
        ArrayList payList = new ArrayList();
        StringBuilder payMsg = new StringBuilder();
        PayList pay = this.getPayList(payls, payList, "03", payMsg);
        if(pay == null) {
            return this.showAlert(payMsg.toString());
        } else {
            String data = this.pssprintDataService.getPssSaleData(((PayList)payList.get(0)).getShopCode(), Double.valueOf(Double.parseDouble(money)), pay.getAmount(), ((PayList)payList.get(0)).getPayType(), pay.getSalesCode(), products, msg);
            if(data == null) {
                return this.showAlert("销售打印发生错误");
            } else {
                ModelAndView mv = new ModelAndView("pss/print/print");
                mv.addObject("data", data);
                mv.addObject("title", "销售单打印");
                mv.addObject("ptjs", "psssale");
                return mv;
            }
        }
    }
}
